package org.jin.samples.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.jin.samples.model.ExamUser;
import org.jin.samples.model.vo.CommonMessageResultVO;
import org.jin.samples.model.vo.ExamUserVO;
import org.jin.samples.service.IManageStudentService;
import org.jin.samples.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class ManageStudentContreller {
		
	@Autowired
	private IManageStudentService manageStudentService;
	
	@RequestMapping(value = "users")
	public String findAllUser(Model model){
		List<ExamUser> userList= manageStudentService.findAllUser();
		model.addAttribute("userList", userList);
		return "user/user";	
	}
	
	@RequestMapping(value = "/findByIdUser/{id}", method=RequestMethod.POST)
	public String findByIdUser(@PathVariable String id){
		try {
			manageStudentService.findByIdUser(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/users";
	}
	
	@ResponseBody
	@RequestMapping(value = "/createUser", method=RequestMethod.POST)
	public CommonMessageResultVO createUser(Model model,ExamUserVO vo){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		resultVO.setSuccess(false);
		try {
			manageStudentService.create(vo);
			resultVO.setSuccess(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return resultVO;
	}
	
	@ResponseBody
	@RequestMapping(value = "/toedit", method = RequestMethod.POST)
	public ExamUserVO toedit(String userId){
		try {
			ExamUserVO  uservo = manageStudentService.findByIdUser(userId);
			return uservo;	
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@ResponseBody
	@RequestMapping(value = "/editUser", method=RequestMethod.POST )
	public CommonMessageResultVO editUser(ExamUserVO vo){
		CommonMessageResultVO redirect = new CommonMessageResultVO();
		redirect.setSuccess(false);
		try {
			manageStudentService.Updata(vo);
			redirect.setSuccess(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return redirect;
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete", method=RequestMethod.POST )
	public CommonMessageResultVO delete(String userId){
		CommonMessageResultVO redirect = new CommonMessageResultVO();
			redirect.setSuccess(false);
		try {
			manageStudentService.delete(userId);
			redirect.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return redirect;
	}
	
	@ResponseBody
	@RequestMapping(value="/uploadFile", method=RequestMethod.POST)
	public CommonMessageResultVO uploadFile(@RequestParam(value = "uploadFile") MultipartFile[] uploadFile, HttpSession session, Model model){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		MultipartFile uploadSimpleFile = uploadFile[0];
		String fileName = uploadSimpleFile.getOriginalFilename();
		
		//获取文件后缀名
		String postfix = fileName.substring(fileName.lastIndexOf("."));
		Map<String, String> messageMap = new HashMap<String,String>(); 	
		//System.out.println("postfix="+postfix);
		
		try{
		if(ExcelUtils.OFFICE_EXCEL_2007_POSTFIX.equals(postfix) || ExcelUtils.OFFICE_EXCEL_2003_POSTFIX.equals(postfix)){
			manageStudentService.saveStudentByExcel(uploadSimpleFile.getInputStream(), postfix);
			resultVO.setSuccess(true);
		}else {
			messageMap.put("message", "文件格式不正确.");
			resultVO.setMessageMap(messageMap);
		}
	} catch(Exception e){
			e.printStackTrace();
			messageMap.put("message", "批量导入失败");
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
	
}
